# 同行


class Together:
    # 最小人数 时间段
    def __init__(self, mongo_client, redis_client, together_time, together_people_count):
        # self.data = {}  # camera_id  [(acvs_id, timestamp)]
        self.redis_client = redis_client
        self.mongo_client = mongo_client
        self.together_time = together_time
        self.together_people_count = together_people_count
        self.data_latest_old_time = {}
        # self.latest_time = 0   区分摄像头
        # self.old_time = 0
        self.data_time = {}  # camera_id: [timestamp]
        self.data_acvs = {}  # camera_id: [acvs_id]
        self.data_acvs_time = {}  # camera_id: [{acvs_id: (time, face_id) }]
        # self.data_acvs_face ={}   # camera_id: [{acvs_id: face}]

    def handel_logic(self, camera_id, timestamp, acvs_id, face_id):
        # 判断摄像头是否存在 以及最近的时间是否过低或者过高
        self.get_camera(camera_id)
        self.get_acvs_id(camera_id, acvs_id, timestamp, face_id)

    def get_camera(self, camera_id):
        #     if self.data.get(camera_id) is None:
        #         self.data.update({camera_id: []})
        if self.data_time.get(camera_id) is None:
            self.data_time.update({camera_id: []})
        if self.data_acvs.get(camera_id) is None:
            self.data_acvs.update({camera_id: []})
        if self.data_acvs_time.get(camera_id) is None:
            self.data_acvs_time.update({camera_id: {}})
        if self.data_latest_old_time.get(camera_id) is None:
            self.data_latest_old_time.update({camera_id: {'latest_time': 0, 'old_time': 0}})

    def get_acvs_id(self, camera_id, acvs_id, timestamp, face_id):
        acvs_list = self.redis_client.get_acvs_acvs(acvs_id)
        if len(acvs_list) == 0:
            new_acvs_id = acvs_id
        else:
            acvs_list.append(acvs_id)
            acvs_map = {}
            # 需要替换掉的acvs
            # deal_acvs = []
            for acvs_id_one in acvs_list:
                # if acvs_id_one in self.data_acvs.get(camera_id):
                #     deal_acvs.append(acvs_id_one)

                count = self.redis_client.get_acvs_face_count(acvs_id_one)
                acvs_map[acvs_id_one] = count

            acvs_map_list = sorted(acvs_map.items(), key=lambda d: d[1], reverse=True)
            # p_id
            new_acvs_id = acvs_map_list[0][0]

            # 强行替换档案和时间?

        # 判断new_acvs_id
        if new_acvs_id in self.data_acvs.get(camera_id):
            # # 替换时间 就要判断时间
            # pre_time = self.data_acvs_time.get(camera_id).get(new_acvs_id)
            # if (new_acvs_id > pre_time) and
            # 先不做处理 第一次进来的时间
            pass
        else:
            self.get_time(camera_id, timestamp, new_acvs_id, face_id)

    def get_time(self, camera_id, timestamp, acvs_id, face_id):
        if len(self.data_time.get(camera_id)) == 0:
            self.data_latest_old_time.get(camera_id).update({'latest_time': timestamp})
            self.data_latest_old_time.get(camera_id).update({'old_time': timestamp})
            self.data_time.get(camera_id).append(timestamp)
            self.data_acvs.get(camera_id).append(acvs_id)
            self.data_acvs_time.get(camera_id).update({acvs_id: (timestamp, face_id)})

        else:
            if timestamp < self.data_latest_old_time.get(camera_id).get('old_time'):
                # 不做处理
                pass

            elif timestamp <= self.data_latest_old_time.get(camera_id).get('latest_time'):
                # 时间段内同行
                pass
                # self.data_time.get(camera_id).append(timestamp)
                # self.data_acvs.get(camera_id).append(acvs_id)
                # self.data_acvs_time.get(camera_id).update({acvs_id: (timestamp, face_id)})
                #
                # # 插入mongo
                # if len(self.data_acvs.get(camera_id)) >= self.together_people_count:
                #     self.insert_data(camera_id, acvs_id, face_id, timestamp)

            elif self.data_latest_old_time.get(camera_id).get(
                    'latest_time') <= timestamp <= self.data_latest_old_time.get(
                camera_id).get('latest_time') + self.together_time:
                # 清一段时间 同行 添加自己
                self.data_latest_old_time.get(camera_id).update({'latest_time': timestamp})
                self.data_time.get(camera_id).append(timestamp)
                self.data_acvs.get(camera_id).append(acvs_id)
                self.data_acvs_time.get(camera_id).update({acvs_id: (timestamp, face_id)})
                new_old_time = self.data_latest_old_time.get(camera_id).get('latest_time') - self.together_time
                self.data_latest_old_time.get(camera_id).update({'old_time': new_old_time})

                delete_acvs_list = []

                for acvs_key, time_value_face in self.data_acvs_time.get(camera_id).items():
                    time_value = time_value_face[0]
                    # face_value = time_value_face[1]
                    if time_value < self.data_latest_old_time.get(camera_id).get('old_time'):
                        self.data_acvs.get(camera_id).remove(acvs_key)
                        self.data_time.get(camera_id).remove(time_value)
                        delete_acvs_list.append(acvs_key)

                for delete_acvs in delete_acvs_list:
                    self.data_acvs_time.get(camera_id).pop(delete_acvs)

                # 插入mongo
                if len(self.data_acvs.get(camera_id)) >= self.together_people_count:
                    self.insert_data(camera_id, acvs_id, face_id, timestamp)

            if timestamp > self.data_latest_old_time.get(camera_id).get('latest_time') + self.together_time:
                # 清空所有 并添加自己
                self.data_acvs.get(camera_id).clear()
                self.data_time.get(camera_id).clear()
                self.data_acvs_time.get(camera_id).clear()

                self.data_latest_old_time.get(camera_id).update({'latest_time': timestamp})
                new_old_time = self.data_latest_old_time.get(camera_id).get('latest_time') - self.together_time
                self.data_latest_old_time.get(camera_id).update({'old_time': new_old_time})

                self.data_time.get(camera_id).append(timestamp)
                self.data_acvs.get(camera_id).append(acvs_id)
                self.data_acvs_time.get(camera_id).update({acvs_id: (timestamp, face_id)})

    def insert_data(self, camera_id, acvs_id, face_id, timestamp):
        acvs_list = self.data_acvs.get(camera_id)
        for o_acvs_id in acvs_list:
            if o_acvs_id == acvs_id:
                continue

            o_timestamp = self.data_acvs_time.get(camera_id).get(o_acvs_id)[0]
            o_face_id = self.data_acvs_time.get(camera_id).get(o_acvs_id)[1]

            o_url = self.redis_client.get_face(o_face_id)['face_image_uri']
            t_url = self.redis_client.get_face(face_id)['face_image_uri']

            data_dic = {
                "o_acvs_id": o_acvs_id,
                "o_face_id": o_face_id,
                # "o_p": o_acvs_id,
                "o_timestamp": o_timestamp,
                "o_face_image_uri": o_url,
                "t_acvs_id": acvs_id,
                "t_face_id": face_id,
                # "t_p": acvs_id,
                "t_timestamp": timestamp,
                "t_face_image_uri": t_url,
                "interval": abs(int(timestamp - o_timestamp)),
                "camera_id": camera_id
                # "timestamp": self.data_acvs_time.get(camera_id).get(acvs_id)
            }

            self.mongo_client.ins_together(data_dic)

            data_dic2 = {
                "t_acvs_id": o_acvs_id,
                "t_face_id": o_face_id,
                # "o_p": o_acvs_id,
                "t_timestamp": o_timestamp,
                "t_face_image_uri": o_url,
                "o_acvs_id": acvs_id,
                "o_face_id": face_id,
                # "t_p": acvs_id,
                "o_timestamp": timestamp,
                "o_face_image_uri": t_url,
                "interval": abs(int(timestamp - o_timestamp)),
                "camera_id": camera_id
                # "timestamp": self.data_acvs_time.get(camera_id).get(acvs_id)
            }

            self.mongo_client.ins_together(data_dic2)
